/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.course.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.course.entity.CourseCategoryEntity;
import org.springblade.modules.material.vo.TreeModel;
import org.springblade.modules.material.vo.TreeVo;
import org.springblade.modules.course.vo.CourseCategoryVO;
import org.springblade.modules.course.mapper.CourseCategoryMapper;
import org.springblade.modules.course.service.ICourseCategoryService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.List;

/**
 * 教具分类 服务实现类
 *
 * @author junxin
 * @since 2023-03-22
 */
@Service
public class CourseCategoryServiceImpl extends BaseServiceImpl<CourseCategoryMapper, CourseCategoryEntity> implements ICourseCategoryService {


	@Override
	public IPage<CourseCategoryVO> selectCourseCategoryPage(IPage<CourseCategoryVO> page, CourseCategoryVO courseCategory) {
		return page.setRecords(baseMapper.selectCourseCategoryPage(page, courseCategory));
	}

	@Override
	public boolean deleteByIds(String ids) {
		Long cnt = baseMapper.selectCount(Wrappers.<CourseCategoryEntity>query().lambda().eq(CourseCategoryEntity::getIsDeleted, 0).in(CourseCategoryEntity::getFatherId, Func.toLongList(ids)));
		if (cnt > 0) {
			throw new ServiceException("请先删除子节点的分类!");
		}
		return removeByIds(Func.toLongList(ids));
	}


	@Override
	public List<TreeVo> treeList(TreeModel model) {
		//1.获取所有一级分类
		model.setParentId("0");
		List<TreeVo> oneList = baseMapper.treeList(model);
		if (CollectionUtil.isEmpty(oneList)) return new ArrayList<>();

		if (model.getLevel() >= 2) {//需要二级列表再去拿

			oneList.stream().forEach(one -> {
				//2.获取每个对应的二级分类
				List<TreeVo> twoList = baseMapper.treeList(new TreeModel(null, one.getId()));
				one.setChildren(twoList);
				one.setParentId("0");
				if (CollectionUtils.isNotEmpty(twoList)) one.setHasChildren(true);
				if (model.getLevel()  >= 3) {//需要三级列表再去拿
					twoList.stream().forEach(two -> {
						//3.获取每个对应的三级分类
						List<TreeVo> threeList = baseMapper.treeList(new TreeModel(null, two.getId()));
						two.setChildren(threeList);
						two.setParentId(one.getId());
						if (CollectionUtils.isNotEmpty(threeList)) two.setHasChildren(true);
					});
				}
			});

		}
		return oneList;
	}

}
